import * as storeService from '../services/storeService';
import * as franService from '../services/franService';
import { decryptRes, dealPagination } from '../utils/requestUtil';
import { message } from 'antd';

export default {
  namespace: 'store',

  state: {
    storeList: {},
    storeListPagination: {},
    storeDetail: {},
    qrCode:{},
    franList:[],
  },

  effects: {
    *fetchStoreList({ payload }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(storeService.fetchStoreList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'storeListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *updateStoreSts({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(storeService.updateStoreSts, payload)));
      console.log(response)
      if (response.code === 0) {
        message.success('修改成功')
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },

    *delStore({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(storeService.delStore, payload)));
      if (response.code === 0) {
        message.success('删除成功')
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
      if (callback) callback();
    },
    *addStore({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(storeService.addStore, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;
        message.success('添加成功！')
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    *getStoreDetail({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(storeService.getStoreDetails, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'storeDetailReducer',
          payload: { data },
        });
        if (callback) { callback() }
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editStore({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(storeService.updateStore, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        let data = response.data;

        message.success('修改成功！')
        console.log('添加后返回的数据：', data.storeDetail);

        if (callback) callback(data.storeDetail);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *getFranchiserQRCode({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = yield call(storeService.getFranchiserQRCode, payload);
      console.log('接口返回数据：',response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'qrCodeReducer',
          payload: { data },
        });
        if (callback) { callback() }
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    *fetchFranList({ payload }, { call, put }) {
      console.log('前端参数11111121212', payload)
      const response = JSON.parse(decryptRes(yield call(franService.fetchFranList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'franListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
    
  },

  reducers: {
    storeListReducer(state, action) {
      //console.log(action.payload.data)
      return {
        ...state,
        storeList: action.payload.data,
        //storeListPagination: dealPagination(action.payload.data.pageInfo),
      };
    },
    updateStoreStsReducer(state, action) {
      //console.log(action.payload.data)
      return {
        ...state,
        storeList: action.payload.data,
      };
    },
    storeDetailReducer(state, action) {
      //console.log(action.payload.data)
      return {
        ...state,
        storeDetail: action.payload.data,
      };
    },
    qrCodeReducer(state, action){
      console.log(action.payload)
      return {
        ...state,
        qrCode: action.payload.data,
      };
    },
   franListReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        franList: action.payload.data.dataList,
      };
    },
  },
};
